package com.ssb.plugins;

import com.apache.api.vo.ResultEntity;
import com.apache.cache.util.Validator;
import com.apache.ius.plugin.CustomMethodPlugin;
import com.apache.ius.plugin.IusPluginUtil;
import com.apache.tools.DataMap;
import com.apache.tools.DateUtils;
import org.apache.log4j.Logger;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Classname ChannelAuditInitProPluginImpl
 * @Description 会员申请渠道商审核通过初始化
 * @Date 2020/3/29 22:21
 * @Created by fpc
 */
public class ChannelAuditInitProPluginImpl implements CustomMethodPlugin {
    private Logger log = Logger.getLogger(getClass());
    @Override
    public Object beforeInvoke(Map<String, Object> map) {
        String auditStatus = String.valueOf(map.get("ssApplyBroker.auditStatus"));
        String applyLevel = String.valueOf(map.get("ssApplyBroker.applyLevel"));
        String userId = String.valueOf(map.get("ssApplyBroker.userId"));
        if("90".equals(auditStatus)){//审核通过
            String newOrgId = String.valueOf(map.get("uctUser.orgId"));//需要修改成为的渠道商ID(new)
            String userEname = String.valueOf(map.get("ssApplyBroker.userEname"));
            String userCname = String.valueOf(map.get("ssApplyBroker.userCname"));
            //查询审核通过的用户名称更新为手机号
           /* Map selectUserMap = new HashMap();
            selectUserMap.put("uctUser.w_userId",userId);
            ResultEntity selectUserEntity = IusPluginUtil.getInstance().select("plateform", "obj", "s_uctUser", selectUserMap);
            if(!Validator.isEmpty(selectUserEntity.getEntity())){
                DataMap dataMap = (DataMap) selectUserEntity.getEntity();
                String mobile = String.valueOf(dataMap.get("mobile"));
                if(Validator.isNotNull(mobile)){
                    userEname= mobile;
                }
            }*/
            if(!"1".equals(applyLevel)){
                //查询审核通过后挂到的渠道商下有哪些产品
                Map selectProMap = new HashMap();
                selectProMap.put("ssOrgProRelation.w_userId",newOrgId);
                ResultEntity selectProEntity = IusPluginUtil.getInstance().select("ssb", "list", "s_ssOrgProRelation", selectProMap);
                String proIds = "";
                if(!Validator.isEmpty(selectProEntity.getEntity())){
                    List<DataMap> selectProList = (List)selectProEntity.getEntity();
                    if(selectProList.size()>0){
                        for (DataMap dataMap: selectProList) {
                            proIds += proIds==""?dataMap.get("proId"):"-#-"+dataMap.get("proId");
                        }
                        log.info("proIds----->>>>"+proIds);
                        //建立产品和被平移的子级渠道商建立关系，同时默认分佣比例为0
                        Map saveMap = new HashMap();
                        saveMap.put("ssOrgProRelation.userId",userId);
                        saveMap.put("ssOrgProRelation.w_userId",userId);
                        saveMap.put("ssOrgProRelation.userOwner",userCname);
                        saveMap.put("ssOrgProRelation.proId",proIds);
                        saveMap.put("ssOrgProRelation.createTime", DateUtils.Now.fmt_yyyyMMdd_HHmmss());
                        saveMap.put("ssOrgProRelation.bkgeRatio","0");
                        saveMap.put("ssOrgProRelation.delStatus","F");
                        saveMap.put("ifBatch","T");
                        saveMap.put("ssOrgProRelation.splitAttr","proId");
                        ResultEntity resultEntity = IusPluginUtil.getInstance().saveInvoke("ssb", "d_ssOrgProRelation,ib_ssOrgProRelation", saveMap);
                        log.info("resultEntity----->>>>>"+resultEntity.toString());
                    }
                }
            }
            //更新用户信息
            Map updateUser = new HashMap();
            updateUser.put("uctUser.w_userId",userId);
            updateUser.put("uctUser.userType",map.get("uctUser.userType"));
            updateUser.put("uctUser.orgId",map.get("uctUser.orgId"));
            updateUser.put("uctUser.orgEname",map.get("uctUser.orgEname"));
            updateUser.put("uctUser.userEname",userEname);
            updateUser.put("uctUser.userLevel",map.get("ssApplyBroker.applyLevel"));
            ResultEntity updateUserEntity = IusPluginUtil.getInstance().saveInvoke("plateform", "u_uctUser", updateUser);
            log.info("updateUserEntity----->>>>>"+updateUserEntity.toString());
            //            IusPluginUtil.getInstance().sendToIusService()
            map.put("beforeMethodKey", "initUserBalance");
            Map sendMap = new HashMap();
            sendMap.put("userId",userId);
            ResultEntity sendResultEntity = IusPluginUtil.getInstance().sendToIusService("ss-user-balance", "addForm", sendMap);
            log.info("sendResultEntity----->>>>"+sendResultEntity.toString());
        }

        //更新审核表
        /**
         * <input type="hidden" name="ssApplyBroker.applyLevel" class="clean"/>
         * <input type="hidden" name="ssApplyBroker.auditStatus" class="clean"/>
         * <input type="hidden" name="ssApplyBroker.w_infoId" d
         */
        Map updateSsApplyBroker = new HashMap();
        updateSsApplyBroker.put("ssApplyBroker.applyLevel",applyLevel);
        updateSsApplyBroker.put("ssApplyBroker.auditStatus",auditStatus);
        updateSsApplyBroker.put("ssApplyBroker.w_infoId",map.get("ssApplyBroker.w_infoId"));
        updateSsApplyBroker.put("ssApplyBroker.auditRemark",map.get("ssApplyBroker.auditRemark"));
        ResultEntity updateSsApplyBrokerEntity = IusPluginUtil.getInstance().saveInvoke("ssb", "u_ssApplyBroker", updateSsApplyBroker);
        log.info("updateSsApplyBrokerEntity----->>>>>"+updateSsApplyBrokerEntity.toString());

        Map<String, Object> result = new HashMap<String, Object>();
        result.put(CustomMethodPlugin.resultMark, "操作成功");
        return result;
    }

    @Override
    public Object doInvoke(Map<String, Object> map) {
        return null;
    }

    @Override
    public Object afterInvoke(Map<String, Object> map) {
        return null;
    }
}
